package cn.kmsoft.stgeometry.util;

import cn.kmsoft.stgeometry.landm.Block;
import cn.kmsoft.stgeometry.landm.Land;
import cn.kmsoft.stgeometry.landm.Point;
import cn.kmsoft.stgeometry.landm.Ring;
import cn.kmsoft.common.utils.FileUtil;
import cn.kmsoft.common.utils.StringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import java.io.*;

//import org.json.JSONArray;
//import org.json.JSONException;
//import org.json.JSONObject;

public class TxtUtil {
    //land-->blocks-->rings-->points
    //ring标识
    private static int oldRingFlag = 0;
    private static String encoding = "GBK";

    /**
     * @param args
     */
    public static void main(String[] args) {
        String root = "E:\\temp\\DHTools\\txt2shp\\TXT\\";
        String txt = "永川61005-2000.txt";
        String geojson = "永川61005-2000.geojson";

        try {
            JSONObject json = txtParse(root + txt);
            String jsonStr = json.toString();
            //String jsonStr = toGeoJson(root+txt);

            System.out.println(jsonStr);
            FileUtil.writeFile(jsonStr, root + geojson);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Txt坐标串解析，GeoJSON字符串(JSONObject)
     *
     * @param txtpath
     * @return
     * @throws IOException
     * @throws JSONException
     */
    public static com.alibaba.fastjson.JSONObject txtParse(String txtpath) throws IOException, JSONException {
        //文件名称，电子监管号或者批复文号
        String fileName = FileUtil.getFileNameWithNoPathAndExtension(txtpath);

        FileInputStream fis = new FileInputStream(txtpath);
        InputStreamReader isr = new InputStreamReader(fis, "gbk");
        BufferedReader reader = new BufferedReader(isr);

        JSONArray features = new JSONArray();
        JSONObject feature = null;
        JSONObject geometry = null;
        JSONArray coordinates = null;
        JSONArray ring = null;
        JSONObject properties = null;

        String line = null;
        boolean begin = false;    // 是否开始读取坐标
        boolean props = false;    // 是否为属性行
        int last = 0;

        while ((line = reader.readLine()) != null) {
            line = line.trim();

            if (StringUtil.isNullOrEmpty(line)) {
                continue;    // 跳过空行
            }
            if (line.endsWith("@")) {
                if (begin) {
                    coordinates.add(ring);    // 添加最后一个环
                    geometry = new JSONObject();
                    geometry.put("type", "Polygon");
                    geometry.put("coordinates", coordinates);
                    // 创建要素
                    feature = new JSONObject();
                    feature.put("type", "feature");
                    feature.put("properties", properties);
                    feature.put("geometry", geometry);
                    features.add(feature);
                } else {
                    begin = true;
                }

                // 创建属性
                //108,1.4424,1,1,面,,,地类编码,@
                //即界址点个数，地块面积，地块编号，地块名称，记录图形属性（点线面），图幅号，地块用途，地类编码，@
                String[] values = line.split(",");
                properties = new JSONObject();
                properties.put("name", values[3]);//地块名称
                properties.put("type", values[6]);//地块用途
                properties.put("bz", fileName);//文件名，电子监管号或者批复文号
                // 创建坐标数组
                coordinates = new JSONArray();
                ring = null;// 上一个要素的最后一个环设置为null，避免被添加到下一个要素中
                // 设置标识位
                props = true;
                last = 0;    // 读取新要素后重置last
            }

            if (props) {    // 属性行不做下一步处理
                props = false;
                continue;
            }

            if (begin) {
                String[] values = line.split(",");
                int i = Integer.parseInt(values[1]);//环的编号
                double y = Double.parseDouble(values[2]);//坐标y
                double x = Double.parseDouble(values[3]);//坐标x

                if (i != last) {
                    if (ring != null) {
                        coordinates.add(ring);    // 将上一个环添加到坐标中
                    }
                    ring = new JSONArray();    // 构造一个新的环
                    last = i;
                }
                JSONArray jsonArray = new JSONArray();
                jsonArray.add(x);
                jsonArray.add(y);
                ring.add(jsonArray);
            }
        }
        coordinates.add(ring);    // 添加最后一个环
        geometry = new JSONObject();
        geometry.put("type", "Polygon");
        geometry.put("coordinates", coordinates);
        // 创建最后一个要素
        feature = new JSONObject();
        feature.put("type", "feature");
        feature.put("properties", properties);
        feature.put("geometry", geometry);
        features.add(feature);
        // 构造GeoJson对象
        JSONObject geojson = new JSONObject();
        geojson.put("type", "FeatureCollection");
        geojson.put("features", features);

        return geojson;
    }

    /**
     * 根据txt生成GeoJSON，只检查txt的坐标
     * 代码摘抄至：捷泰天域杨国勇
     *
     * @param txtpath
     * @param encoding
     * @return  org.json.JSONObject
     * @throws Exception
     */
    public static org.json.JSONObject txtParse(String txtpath, String encoding) throws Exception {
        FileInputStream fis = new FileInputStream(txtpath);
        InputStreamReader isr = new InputStreamReader(fis, encoding);
        BufferedReader reader = new BufferedReader(isr);

        org.json.JSONArray features = new org.json.JSONArray();
        org.json.JSONObject feature = null;
        org.json.JSONObject geometry = null;
        org.json.JSONArray coordinates = null;
        org.json.JSONArray ring = null;
        org.json.JSONObject properties = null;
        String line = null;

        boolean begin = false;	// 是否开始读取坐标
        boolean props = false;	// 是否为属性行
        int last = 0;

        while ((line = reader.readLine()) != null) {
            line = line.trim();

            if (StringUtil.isNullOrEmpty(line)) {
                continue;	// 跳过空行
            }
            if (line.endsWith("@")) {
                if (begin) {
                    coordinates.put(ring);	// 添加最后一个环
                    geometry = new org.json.JSONObject();
                    geometry.put("type", "Polygon");
                    geometry.put("coordinates", coordinates);

                    // 创建要素
                    feature = new org.json.JSONObject();
                    feature.put("type", "Feature");
                    feature.put("properties", properties);
                    feature.put("geometry", geometry);
                    features.put(feature);
                } else {
                    begin = true;
                }
                // 创建属性
                String[] values = line.split(",");
                // 界址点数,地块面积,地块编号,地块名称,记录图形属性(点线面),图幅号,地块用途,地类编码
                int count = 0;
                double area = 0.0;

                if (!StringUtil.isNullOrEmpty(values[0])) {
                    count = Integer.parseInt(values[0]);
                }
                if (!StringUtil.isNullOrEmpty(values[1])) {
                    area = Double.parseDouble(values[1]);
                }
                properties = new org.json.JSONObject()
                        .put("JZDS", count)
                        .put("TXJSMJ", area)
                        .put("DKBH", values[2])
                        .put("ZDMC", values[3])
                        .put("TXLX", values[4])
                        .put("TFH", values[5])
                        .put("TDYT", values[6]);

                // 创建坐标数组
                coordinates = new org.json.JSONArray();
                ring = null;// 上一个要素的最后一个环设置为null，避免被添加到下一个要素中
                // 设置标识位
                props = true;
                last = 0;	// 读取新要素后重置last
            }
            if (props) {	// 属性行不做下一步处理
                props = false;
                continue;
            }
            if (begin) {
                String[] values = line.split(",");
                int i = Integer.parseInt(values[1]);
                double y = Double.parseDouble(values[2]);
                double x = Double.parseDouble(values[3]);

                if (i != last) {
                    if (ring != null) {
                        coordinates.put(ring);	// 将上一个环添加到坐标中
                    }
                    ring = new org.json.JSONArray();	// 构造一个新的环
                    last = i;
                }
                ring.put(new org.json.JSONArray().put(x).put(y));
            }
        }
        coordinates.put(ring);	// 添加最后一个环
        geometry = new org.json.JSONObject();
        geometry.put("type", "Polygon");
        geometry.put("coordinates", coordinates);

        // 创建最后一个要素
        feature = new org.json.JSONObject();
        feature.put("type", "Feature");
        feature.put("properties", properties);
        feature.put("geometry", geometry);
        features.put(feature);

        // 构造GeoJson对象
        org.json.JSONObject geojson = new org.json.JSONObject();
        geojson.put("type", "FeatureCollection");
        geojson.put("features", features);

        // 关闭文件流
        reader.close();
        isr.close();
        fis.close();
        return geojson;
    }

    /**
     * Txt坐标串解析，GeoJSON字符串(Land-block-ring-point)
     *
     * @param txtpath
     * @return
     */
    public static String toGeoJson(String txtpath) {
        Land land = Land.readTxtFile(txtpath);
        String jsonStr = land.toJsonStr();

        return jsonStr;
    }

    /**
     * 读取标准图形坐标串txt文件并转为Land对象：
     * （清华三维导出，fme提取转换）
     * 1、通过TXT中的degreeNum进行设置
     *
     * @param filePath txt文件物理路径
     * @return
     */
    public static Land readTxtFile(String filePath) {
        oldRingFlag = 0;
        Land land = new Land();
        try {
            String encoding = "GBK";
            Block block = null;
            Ring ring = null;

            //读取txt文件
            File file = new File(filePath);
            if ((file.isFile()) && (file.exists())) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);

                //行数
                int lineNum = 0;
                //遍历每一行line，很重要
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    lineNum++;

                    //1、land txt 的基本信息（格式版本号=1.0版本）
                    if (lineTxt.indexOf("=") != -1) {
                        String[] line = lineTxt.split("=");
                        int size = line.length;
                        if (size>1 && (line[0] != null) && (line[0].equals("格式版本号")) ) {
                            land.setFormatVersion(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("数据产生单位")) ) {
                            land.setDataProduceOrg(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("数据产生日期")) ) {
                            land.setProduceDate(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("坐标系")) ) {
                            land.setCoordinateSystem(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("几度分带")) ) {
                            land.setDegreeNo(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("投影类型")) ) {
                            land.setProjectionType(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("计量单位")) ) {
                            land.setUnit(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("带号")) ) {
                            land.setDegreeNum(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("精度")) ) {
                            land.setPrecision(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("转换参数")) ) {
                            land.setConversionParameter(line[1]);
                        }
                    } else {
                        //2、新地块block 信息   1038,59.1068,13,13,面,,,地类编码,@
                        //通过@来分割
                        if (lineTxt.indexOf("@") != -1) {

                            //新建地块---同时新建地块环
                            block = new Block();
                            ring = new Ring();
                            oldRingFlag = 1;

                            //对每一行按照“，”号分割

                            String[] line = lineTxt.split(",");
                            int size = line.length;
                            if (size>0 && !StringUtil.isNullOrEmpty(line[0])) {
                                int jzds = Integer.parseInt(line[0]);
                                block.getbAttrs().put("jzds", jzds);
                                block.setJzds(jzds);
                            }
                            if (size>1 && !StringUtil.isNullOrEmpty(line[1])) {
                                Double dkmj = Double.parseDouble(line[1]);
                                block.getbAttrs().put("dkmj", dkmj);
                                block.setDkmj(dkmj);
                            }
                            if (size>2 && !StringUtil.isNullOrEmpty(line[2])) {
                                String dkbh = line[2];
                                block.getbAttrs().put("dkbh", dkbh);
                                block.setDkbh(dkbh);
                            }
                            if (size>3 && !StringUtil.isNullOrEmpty(line[3])) {
                                String dkmc = line[3];
                                block.getbAttrs().put("dkmc", dkmc);
                                block.setDkmc(dkmc);
                            }
                            if (size>4 && !StringUtil.isNullOrEmpty(line[4])) {
                                String jltxsx = line[4];
                                block.getbAttrs().put("jltxsx", jltxsx);
                                block.setJltxsx(jltxsx);
                            }
                            if (size>5 && !StringUtil.isNullOrEmpty(line[5])) {
                                String tfh = line[5];
                                block.getbAttrs().put("tfh", tfh);
                                block.setTfh(tfh);
                            }
                            if (size>6 && !StringUtil.isNullOrEmpty(line[6])) {
                                String dkyt = line[6];
                                block.getbAttrs().put("dkyt", dkyt);
                                block.setDkyt(dkyt);
                            }

                            block.getRings().add(ring);
                            land.getBlocks().add(block);
                        } else {
                            //分割数据行的数据 J2,1,3297731.838,36371740.460
                            if (lineTxt.indexOf("J") != -1) {

                                String[] attrs = lineTxt.split(",");
                                int sizeOfAttr = attrs.length;
                                //新环标识
                                int newringFlag = Integer.parseInt(attrs[1]);
                                if (newringFlag != oldRingFlag) {
                                    ring = new Ring();
                                    ring.setRingId(newringFlag);

                                    block.getRings().add(ring);
                                    //这个很关键
                                    oldRingFlag = newringFlag;
                                }

                                Point point = new Point();
                                //注意，txt中x为36-xxx，
                                if (sizeOfAttr>2 && !StringUtil.isNullOrEmpty(attrs[2])) {
                                    point.setY(Double.parseDouble(attrs[2]));
                                }
                                if (sizeOfAttr>3 && !StringUtil.isNullOrEmpty(attrs[3])) {
                                    String xstr = attrs[3];
                                    if (attrs[3].indexOf("|") != -1) {
                                        xstr = attrs[3].substring(0, attrs[3].indexOf("|"));
                                    }
                                    point.setX(Double.parseDouble(xstr));
                                }

                                ring.getPoints().add(point);
                            }
                        }
                    }
                }

                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return land;
    }

    /**
     * 读取标准图形坐标串txt文件并转为Land对象
     *
     * 1、通过Y坐标的前2位进行自动判断，自动映射到degreeNum中
     * 2、本方法只是读取了坐标串，不解析元数据描述信息
     *
     *
     * @param filePath  txt文件物理路径
     * @param encoding  文件编码格式
     * @return
     */
    public static Land readTxtFile1(String filePath, String encoding) {
        oldRingFlag = 0;
        boolean degreeNumFlag = true;
        Land land = new Land();
        try {

            Block block = null;
            Ring ring = null;

            //读取txt文件
            File file = new File(filePath);
            if ((file.isFile()) && (file.exists())) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);

                //行数
                int lineNum = 0;
                //遍历每一行line，很重要
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    lineNum++;

                    //2、新地块block 信息   1038,59.1068,13,13,面,,,地类编码,@
                    //通过@来分割
                    if (lineTxt.indexOf("@") != -1) {
                        //新建地块---同时新建地块环
                        block = new Block();
                        ring = new Ring();
                        oldRingFlag = 1;

                        //对每一行按照“，”号分割
                        String[] line = lineTxt.split(",");
                        int size = line.length;
                        if (size>0 && !StringUtil.isNullOrEmpty(line[0])) {
                            int jzds = Integer.parseInt(line[0]);
                            block.getbAttrs().put("jzds", jzds);
                            block.setJzds(jzds);
                        }
                        if (size>1 && !StringUtil.isNullOrEmpty(line[1])) {
                            Double dkmj = Double.parseDouble(line[1]);
                            block.getbAttrs().put("dkmj", dkmj);
                            block.setDkmj(dkmj);
                        }
                        if (size>2 && !StringUtil.isNullOrEmpty(line[2])) {
                            String dkbh = line[2];
                            block.getbAttrs().put("dkbh", dkbh);
                            block.setDkbh(dkbh);
                        }
                        if (size>3 && !StringUtil.isNullOrEmpty(line[3])) {
                            String dkmc = line[3];
                            block.getbAttrs().put("dkmc", dkmc);
                            block.setDkmc(dkmc);
                        }
                        if (size>4 && !StringUtil.isNullOrEmpty(line[4])) {
                            String jltxsx = line[4];
                            block.getbAttrs().put("jltxsx", jltxsx);
                            block.setJltxsx(jltxsx);
                        }
                        if (size>5 && !StringUtil.isNullOrEmpty(line[5])) {
                            String tfh = line[5];
                            block.getbAttrs().put("tfh", tfh);
                            block.setTfh(tfh);
                        }
                        if (size>6 && !StringUtil.isNullOrEmpty(line[6])) {
                            String dkyt = line[6];
                            block.getbAttrs().put("dkyt", dkyt);
                            block.setDkyt(dkyt);
                        }

                        block.getRings().add(ring);
                        land.getBlocks().add(block);
                    } else {
                        //分割数据行的数据 J2,1,3297731.838,36371740.460
                        if (lineTxt.indexOf("J") != -1) {
                            String[] attrs = lineTxt.split(",");
                            int sizeOfAttr= attrs.length;
                            //新环标识
                            int newringFlag = Integer.parseInt(attrs[1]);
                            if (newringFlag != oldRingFlag) {
                                ring = new Ring();
                                ring.setRingId(newringFlag);

                                block.getRings().add(ring);
                                //这个很关键
                                oldRingFlag = newringFlag;
                            }

                            Point point = new Point();
                            //注意，txt中x为36-xxx，
                            if (sizeOfAttr>2 && !StringUtil.isNullOrEmpty(attrs[2])) {
                                point.setY(Double.parseDouble(attrs[2]));
                            }
                            if (sizeOfAttr>3 && !StringUtil.isNullOrEmpty(attrs[3])) {
                                String xstr = attrs[3];
                                if (attrs[3].indexOf("|") != -1) {
                                    xstr = attrs[3].substring(0, attrs[3].indexOf("|"));
                                }
                                //判断投影度带,不支持无度带的数据处理
                                int length = attrs[3].length();
                                int indexOfDot = attrs[3].indexOf(".");
                                if (indexOfDot == -1) {
                                    if (degreeNumFlag == true && attrs[3].substring(0, length - 1).length() == 8) {
                                        String degreeNoStr = attrs[3].substring(0, 2);
                                        land.setDegreeNum(degreeNoStr);
                                        degreeNumFlag = false;
                                    } else if (degreeNumFlag == true && attrs[3].substring(0, indexOfDot).length() == 5) {
                                        land.setDegreeNum("cqdl");
                                        degreeNumFlag = false;
                                    }
                                } else {
                                    if (degreeNumFlag == true && attrs[3].substring(0, indexOfDot).length() == 8) {
                                        String degreeNoStr = attrs[3].substring(0, 2);
                                        land.setDegreeNum(degreeNoStr);
                                        degreeNumFlag = false;
                                    } else if (degreeNumFlag == true && attrs[3].substring(0, indexOfDot).length() == 5) {
                                        land.setDegreeNum("cqdl");
                                        degreeNumFlag = false;
                                    }
                                }

                                point.setX(Double.parseDouble(xstr));
                            }

                            ring.getPoints().add(point);
                        }
                    }
                }

                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return land;
    }


    /**
     * 读取txt生成一个land对象并转为Land对象：
     * 1、通过Y坐标的前2位进行自动判断，自动映射到degreeNum中
     *
     * @param filePath  txt文件物理路径
     * @param encoding  txt文件编码格式
     * @return
     */
    public static Land readTxtFile2(String filePath, String encoding) {
        oldRingFlag = 0;
        boolean degreeNumFlag = true;
        Land land = new Land();
        try {

            Block block = null;
            Ring ring = null;

            //读取txt文件
            File file = new File(filePath);
            if ((file.isFile()) && (file.exists())) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);

                //行数
                int lineNum = 0;
                //遍历每一行line，很重要
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    lineNum++;

                    //1、land txt 的基本信息（格式版本号=1.0版本）
                    if (lineTxt.indexOf("=") != -1) {
                        String[] line = lineTxt.split("=");
                        int size = line.length;
                        if (size>1 &&  (line[0] != null) && (line[0].equals("格式版本号"))) {
                            land.setFormatVersion(line[1]);
                        } else if (size>1 &&  (line[0] != null) && (line[0].equals("数据产生单位")) ) {
                            land.setDataProduceOrg(line[1]);
                        } else if (size>1 &&  (line[0] != null) && (line[0].equals("数据产生日期")) ) {
                            land.setProduceDate(line[1]);
                        } else if (size>1 &&  (line[0] != null) && (line[0].equals("坐标系")) ) {
                            land.setCoordinateSystem(line[1]);
                        } else if (size>1 &&  (line[0] != null) && (line[0].equals("几度分带")) ) {
                            land.setDegreeNo(line[1]);
                        } else if (size>1 &&  (line[0] != null) && (line[0].equals("投影类型")) ) {
                            land.setProjectionType(line[1]);
                        } else if (size>1 &&  (line[0] != null) && (line[0].equals("计量单位")) ) {
                            land.setUnit(line[1]);
                        } else if (size>1 &&  (line[0] != null) && (line[0].equals("带号")) ) {
                            land.setDegreeNum(line[1]);
                        } else if (size>1 &&  (line[0] != null) && (line[0].equals("精度")) ) {
                            land.setPrecision(line[1]);
                        } else if (size>1 &&  (line[0] != null) && (line[0].equals("转换参数")) ) {
                            land.setConversionParameter(line[1]);
                        }
                    } else {
                        //2、新地块block 信息   1038,59.1068,13,13,面,,,地类编码,@
                        //通过@来分割
                        if (lineTxt.indexOf("@") != -1) {
                            //新建地块---同时新建地块环
                            block = new Block();
                            ring = new Ring();
                            oldRingFlag = 1;

                            //对每一行按照“，”号分割
                            String[] line = lineTxt.split(",");
                            int size = line.length;
                            if (size>0 && !StringUtil.isNullOrEmpty(line[0])) {
                                int jzds = Integer.parseInt(line[0]);
                                block.getbAttrs().put("jzds", jzds);
                                block.setJzds(jzds);
                            }
                            if (size>1 && !StringUtil.isNullOrEmpty(line[1])) {
                                Double dkmj = Double.parseDouble(line[1]);
                                block.getbAttrs().put("dkmj", dkmj);
                                block.setDkmj(dkmj);
                            }
                            if (size>2 && !StringUtil.isNullOrEmpty(line[2])) {
                                String dkbh = line[2];
                                block.getbAttrs().put("dkbh", dkbh);
                                block.setDkbh(dkbh);
                            }
                            if (size>3 && !StringUtil.isNullOrEmpty(line[3])) {
                                String dkmc = line[3];
                                block.getbAttrs().put("dkmc", dkmc);
                                block.setDkmc(dkmc);
                            }
                            if (size>4 && !StringUtil.isNullOrEmpty(line[4])) {
                                String jltxsx = line[4];
                                block.getbAttrs().put("jltxsx", jltxsx);
                                block.setJltxsx(jltxsx);
                            }
                            if (size>5 && !StringUtil.isNullOrEmpty(line[5])) {
                                String tfh = line[5];
                                block.getbAttrs().put("tfh", tfh);
                                block.setTfh(tfh);
                            }
                            if (size>6 && !StringUtil.isNullOrEmpty(line[6])) {
                                String dkyt = line[6];
                                block.getbAttrs().put("dkyt", dkyt);
                                block.setDkyt(dkyt);
                            }

                            block.getRings().add(ring);
                            land.getBlocks().add(block);
                        } else {
                            //分割数据行的数据 J2,1,3297731.838,36371740.460
                            if (lineTxt.indexOf("J") != -1) {
                                String[] attrs = lineTxt.split(",");
                                int sizeOfAttr = attrs.length;
                                //新环标识
                                int newringFlag = Integer.parseInt(attrs[1]);
                                if (newringFlag != oldRingFlag) {
                                    ring = new Ring();
                                    ring.setRingId(newringFlag);

                                    block.getRings().add(ring);
                                    //这个很关键
                                    oldRingFlag = newringFlag;
                                }

                                Point point = new Point();
                                //注意，txt中x为36-xxx，
                                if (sizeOfAttr>2 && !StringUtil.isNullOrEmpty(attrs[2])) {
                                    point.setY(Double.parseDouble(attrs[2]));
                                }
                                if (sizeOfAttr>3 && !StringUtil.isNullOrEmpty(attrs[3])) {
                                    String xstr = attrs[3];
                                    if (attrs[3].indexOf("|") != -1) {
                                        xstr = attrs[3].substring(0, attrs[3].indexOf("|"));
                                    }
                                    //判断投影度带,不支持无度带的数据处理
                                    int length = attrs[3].length();
                                    int indexOfDot = attrs[3].indexOf(".");
                                    if (indexOfDot == -1) {
                                        if (degreeNumFlag == true && attrs[3].substring(0, length - 1).length() == 8) {
                                            String degreeNoStr = attrs[3].substring(0, 2);
                                            land.setDegreeNum(degreeNoStr);
                                            degreeNumFlag = false;
                                        } else if (degreeNumFlag == true && attrs[3].substring(0, indexOfDot).length() == 5) {
                                            land.setDegreeNum("cqdl");
                                            degreeNumFlag = false;
                                        }
                                    } else {
                                        if (degreeNumFlag == true && attrs[3].substring(0, indexOfDot).length() == 8) {
                                            String degreeNoStr = attrs[3].substring(0, 2);
                                            land.setDegreeNum(degreeNoStr);
                                            degreeNumFlag = false;
                                        } else if (degreeNumFlag == true && attrs[3].substring(0, indexOfDot).length() == 5) {
                                            land.setDegreeNum("cqdl");
                                            degreeNumFlag = false;
                                        }
                                    }

                                    point.setX(Double.parseDouble(xstr));
                                }

                                ring.getPoints().add(point);
                            }
                        }
                    }
                }

                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return land;
    }

    /**
     * 读取txt生成一个land对象并转为Land对象：
     * 1、通过Y坐标的前2位进行自动判断，自动映射到degreeNum中
     * 2、加入了36500000与36000000的区别
     *
     * @param filePath  txt文件物理路径
     * @return
     */
    public static Land readTxtFile3(String filePath) {
        oldRingFlag = 0;
        boolean degreeNumFlag = true;
        Land land = new Land();
        try {
            String encoding = "GBK";
            Block block = null;
            Ring ring = null;

            //读取txt文件
            File file = new File(filePath);
            if ((file.isFile()) && (file.exists())) {
                InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);

                //行数
                int lineNum = 0;
                //遍历每一行line，很重要
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    lineNum++;

                    //1、land txt 的基本信息（格式版本号=1.0版本）
                    if (lineTxt.indexOf("=") != -1) {
                        String[] line = lineTxt.split("=");
                        int size = line.length;
                        if (size>1 && (line[0] != null) && (line[0].equals("格式版本号")) ) {
                            land.setFormatVersion(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("数据产生单位")) ) {
                            land.setDataProduceOrg(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("数据产生日期")) ) {
                            land.setProduceDate(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("坐标系")) ) {
                            land.setCoordinateSystem(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("几度分带")) ) {
                            land.setDegreeNo(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("投影类型")) ) {
                            land.setProjectionType(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("计量单位")) ) {
                            land.setUnit(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("带号")) ) {
                            land.setDegreeNum(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("精度")) ) {
                            land.setPrecision(line[1]);
                        } else if (size>1 && (line[0] != null) && (line[0].equals("转换参数")) ) {
                            land.setConversionParameter(line[1]);
                        }
                    } else {
                        //2、新地块block 信息   1038,59.1068,13,13,面,,,地类编码,@
                        //通过@来分割
                        if (lineTxt.indexOf("@") != -1) {

                            //新建地块---同时新建地块环
                            block = new Block();
                            ring = new Ring();
                            oldRingFlag = 1;

                            //对每一行按照“，”号分割
                            String[] line = lineTxt.split(",");
                            int size = line.length;
                            if (size>0 && !StringUtil.isNullOrEmpty(line[0])) {
                                int jzds = Integer.parseInt(line[0]);
                                block.getbAttrs().put("jzds", jzds);
                                block.setJzds(jzds);
                            }
                            if (size>1 && !StringUtil.isNullOrEmpty(line[1])) {
                                Double dkmj = Double.parseDouble(line[1]);
                                block.getbAttrs().put("dkmj", dkmj);
                                block.setDkmj(dkmj);
                            }
                            if (size>2 && !StringUtil.isNullOrEmpty(line[2])) {
                                String dkbh = line[2];
                                block.getbAttrs().put("dkbh", dkbh);
                                block.setDkbh(dkbh);
                            }
                            if (size>3 && !StringUtil.isNullOrEmpty(line[3])) {
                                String dkmc = line[3];
                                block.getbAttrs().put("dkmc", dkmc);
                                block.setDkmc(dkmc);
                            }
                            if (size>4 && !StringUtil.isNullOrEmpty(line[4])) {
                                String jltxsx = line[4];
                                block.getbAttrs().put("jltxsx", jltxsx);
                                block.setJltxsx(jltxsx);
                            }
                            if (size>5 && !StringUtil.isNullOrEmpty(line[5])) {
                                String tfh = line[5];
                                block.getbAttrs().put("tfh", tfh);
                                block.setTfh(tfh);
                            }
                            if (size>6 && !StringUtil.isNullOrEmpty(line[6])) {
                                String dkyt = line[6];
                                block.getbAttrs().put("dkyt", dkyt);
                                block.setDkyt(dkyt);
                            }

                            block.getRings().add(ring);
                            land.getBlocks().add(block);
                        } else {
                            //分割数据行的数据 J2,1,3297731.838,36371740.460
                            if (lineTxt.indexOf("J") != -1) {

                                String[] attrs = lineTxt.split(",");
                                int sizeOfAttr = attrs.length;
                                //新环标识
                                int newringFlag = Integer.parseInt(attrs[1]);
                                if (newringFlag != oldRingFlag) {
                                    ring = new Ring();
                                    ring.setRingId(newringFlag);

                                    block.getRings().add(ring);
                                    //这个很关键
                                    oldRingFlag = newringFlag;
                                }

                                Point point = new Point();
                                //注意，txt中x为36-xxx，
                                if (sizeOfAttr>2 && !StringUtil.isNullOrEmpty(attrs[2])) {
                                    point.setY(Double.parseDouble(attrs[2]));
                                }
                                if (sizeOfAttr>3 && !StringUtil.isNullOrEmpty(attrs[3])) {
                                    String xstr = attrs[3];
                                    if (attrs[3].indexOf("|") != -1) {
                                        xstr = attrs[3].substring(0, attrs[3].indexOf("|"));
                                    }

                                    int length = attrs[3].length();
                                    int indexOfDot = attrs[3].indexOf(".");
                                    if (indexOfDot == -1) {
                                        if (degreeNumFlag == true && attrs[3].substring(0, length - 1).length() == 8) {
                                            String degreeNoStr = attrs[3].substring(0, 2);
                                            land.setDegreeNum(degreeNoStr);
                                            degreeNumFlag = false;
                                        }
                                    } else {
                                        if (degreeNumFlag == true && attrs[3].substring(0, indexOfDot).length() == 8) {
                                            String degreeNoStr = attrs[3].substring(0, 2);
                                            land.setDegreeNum(degreeNoStr);
                                            degreeNumFlag = false;
                                        }
                                    }

                                    //X坐标的偏移量设置
                                    double xoffset = 0;
                                    if (indexOfDot == -1) {
                                        if (attrs[3].substring(0, length - 1).length() == 8) {
                                            //X坐标的偏移设置
                                            String strXOffset = attrs[3].substring(2, 3);
                                            if (Double.parseDouble(strXOffset) > 5) {
                                                xoffset = 500000;
                                            }
                                        }
                                    } else {
                                        if (attrs[3].substring(0, indexOfDot).length() == 8) {
                                            String strXOffset = attrs[3].substring(2, 3);
                                            if (Double.parseDouble(strXOffset) > 5) {
                                                xoffset = 500000;
                                            }
                                        }
                                    }

                                    point.setX(Double.parseDouble(xstr) - xoffset);
                                }

                                ring.getPoints().add(point);
                            }
                        }
                    }
                }

                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return land;
    }

}